﻿using DefaultDspPlugins.UI.Common.Modulizer.Engine.GUIv2.FrontEndComponents;
using ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine;
using ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine.Modules.HighLevel;
using ZquenceStudio3Host.Graphics.Components;

namespace DefaultDspPlugins.UI.Common.Modulizer.Engine.GUI.Modules
{
    [ModuleUI(typeof(CustomAlgorithmModule))]
    class CustomAlgorithmModuleUI : BaseModuleUI
    {
        public CustomAlgorithmModuleUI(BaseModule module) : base(module)
        {
        }

        protected override string OnSetLayout(BaseModule module)
        {
            mAlgorithmModule = (CustomAlgorithmModule)module;

            string defaultUI =
@"
[ModuleUIPanel]
{
	Name = ModuleUIPanel333;
	[Properties]
	{
		ExpR = ifequal(truncate(truncate(X/4)%63),0,0.7,sin(X*0.01+X*0.01)*0.1+0.6);
		ExpG = 0.1;
		ExpB = ifequal(truncate(truncate(X/4)%63),0,0.6,sin(X*0.01+X*0.01)*0.1+0.5);
		Dimension procent = True;
		MinSize = 0 0;
		MaxSize = 10000000000 10000000000;
		Bg Color = 255 255 255 255;
		Overlay texture = NULL;
		Overlay texture color = 255 255 255 255;
		Name = ModuleUIPanel333;
		TagName = ;
		Position = 0 0 0;
		Rotation = 0 0 0;
		Scale = 100 100 0;
		Visible = True;
	}
    [Components]
    {
        [Label]
		{
			Name = LabelName;
			[Properties]
			{
				Text = Algorithm:;
				Text color = 255 255 255 255;
				Text size = 16;
				Text auto = True;
				Dimension procent = True;
				MinSize = 0 0;
				MaxSize = 1E+10 1E+10;
				Bg Color = 255 0 0 0;
				Texture = NULL;
				Overlay texture = NULL;
				Overlay texture color = 255 255 255 255;
				Name = LabelName;
				Position = 0 20 0;
				Rotation = 0 0 0;
				Scale = 100 20 0;
				Visible = True;
			}
		}
        [Textbox]
		{
			Name = LabelName;
			[Properties]
			{
				Text = {Name};
				Text color = 255 255 255 255;
				Text size = 16;
				Text auto = True;
				Dimension procent = True;
				MinSize = 0 0;
				MaxSize = 1E+10 1E+10;
				Bg Color = 0 0 0 255;
				Texture = NULL;
				Overlay texture = NULL;
				Overlay texture color = 255 255 255 255;
				Name = LabelName;
				TagName = AlgorithmTextbox;
				Position = 0 40 0;
				Rotation = 0 0 0;
				Scale = 100 20 0;
				Visible = True;
			}
		}
    }
}
".Replace("{Name}", mAlgorithmModule.Algorithm);

            return defaultUI;
        }

        private CustomAlgorithmModule mAlgorithmModule;
        private Textbox mTextbox;

        protected override void OnUICreated(ModuleUIPanel uiPanel)
        {
            // Get textbox.
            mTextbox = uiPanel.FindByTag<Textbox>("AlgorithmTextbox");
            mTextbox.OnTextChanged += MTextbox_OnTextChanged;
        }

        protected override void OnUIDestoyed(ModuleUIPanel uiPanel)
        {
            mTextbox.OnTextChanged -= MTextbox_OnTextChanged;
            mTextbox = null;
        }

        private void MTextbox_OnTextChanged(string text)
        {
            mAlgorithmModule.Algorithm = text;
        }
    }
}
